package com.wn.ziroombackend.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.ziroombackend.entity.Cleaner;
import com.wn.ziroombackend.entity.Production;
import com.wn.ziroombackend.entity.vo.AddProduction;
import com.wn.ziroombackend.entity.vo.ProductionVo;
import com.wn.ziroombackend.service.ImageService;
import com.wn.ziroombackend.service.ProductionService;
import com.wn.ziroombackend.utils.FtpUtil;
import com.wn.ziroombackend.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangzhijia
 * @since 2023-02-27 10:56:29
 */
@RestController
@RequestMapping("/production")
public class ProductionController {

    private ProductionService productionService;
    private ImageService imageService;

    @Autowired
    public void setImageService(ImageService imageService) {
        this.imageService = imageService;
    }

    @Autowired
    public void setProductionService(ProductionService productionService) {
        this.productionService = productionService;
    }

    @GetMapping("/searchProduction")
    public R searchProduction(@RequestParam(value = "name") String name,
                              @RequestParam(value = "categoryInfo") String categoryInfo,
                              @RequestParam(value = "pageNum") int pageNum,
                              @RequestParam(value = "pageSize") int pageSize){

        System.out.println("name:"+name+",pageNum:"+pageNum+",pageSize:"+pageSize);

        QueryWrapper<Production> wrapper = new QueryWrapper<>();


        if (!name.equals("")) {
            wrapper.like("production_name", name);
        }
        if (!categoryInfo.equals("")){
            wrapper.eq("category_id",categoryInfo);
        }

        PageHelper.startPage(pageNum, pageSize);
        List<Production> list = productionService.list(wrapper);

        System.out.println(list);

        return R.ok("ok").data("list", new PageInfo<>(list));
    }

    @PostMapping("/delBatch")
    public R delBatch(String ids){

        String[] split = ids.split(",");

        List<Object> list = new ArrayList<>();
        for (String s : split) {
            System.out.println("id--->"+s);
            list.add(s);
        }


        return R.ok("ok").data("ok","删除成功!");
    }

    @PostMapping("/searchById")
    public R searchById(@RequestParam("id") String id){

        ProductionVo productionVo = productionService.findVoById(id);

        AddProduction addProduction = new AddProduction();
        addProduction.setProductionName(productionVo.getProductionName());
        addProduction.setProductionId(productionVo.getProductionId());
        addProduction.setProductionPrice(productionVo.getProductionPrice());
        addProduction.setProductionQuantity(productionVo.getProductionQuantity());
        addProduction.setProductionIntroduction(productionVo.getProductionIntroduction());
        addProduction.setCategoryId(productionVo.getCategory().getCategoryId());
        addProduction.setImage(productionVo.getImageList().get(0).getImageUrl());
        addProduction.setImageId(productionVo.getImageList().get(0).getImageId());


        return R.ok("ok").data("productionVo",addProduction);
    }

    @PostMapping("/uploadImg")
    public R uploadImg(@RequestParam(value = "myHeader") MultipartFile multipartFile) {

        //获取文件上传的名字
        String filename = multipartFile.getOriginalFilename();

        //修改文件名
        String uuid = UUID.randomUUID().toString().replace("-", "");

        //获取文件后缀
        String subfix = filename.substring(filename.lastIndexOf("."));

        //给文件重命名
        filename = "Production_" + uuid + subfix;


        System.out.println("照片的名字为:" + filename);

        try (InputStream inputStream = multipartFile.getInputStream()){
            //创建上传对象
            FtpUtil.uploadFile("139.224.186.148", 21, "781974288", "ekwJdKiTWSFwG2YZ", "/",
                    "/", filename, inputStream);


            return R.ok("ok").data("filename",filename);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/addProduction")
    public R addProduction(@RequestBody AddProduction addProduction){

        System.out.println("productionVo ------------->"+addProduction);


        int productionId = productionService.addNewProduction(addProduction);

        String image = addProduction.getImage();
        int i = imageService.addNewImage(productionId,image);
        return R.ok("ok");
    }

    @PostMapping("/editProduction")
    public R editProduction(@RequestBody AddProduction addProduction){

        int i = productionService.editProduction(addProduction);

        Integer imageId = addProduction.getImageId();
        String image = addProduction.getImage();
        int x = imageService.editImage(imageId,image);

        return R.ok("ok");
    }

}

